import Vuex from 'vuex'
import Vue from 'vue'
import state from './state'

Vue.use(Vuex)

export default new Vuex.Store({
  namespaced: false,
  state,
  mutations: {
    // 登录成功将, token保存在localStorage和sessionStorage中
    login: (state, info) => {
      localStorage.token = info.token
      sessionStorage.token = info.token
      state.token = info.token
      localStorage.user = info.user
      sessionStorage.user = info.user
      state.user = info.user
    },
    // 退出登录将, token清空
    logout: (state) => {
      localStorage.removeItem('token')
      sessionStorage.removeItem('token')
      state.token = null
      localStorage.removeItem('user')
      sessionStorage.removeItem('user')
      state.user = null
    }
    // setDatasetCurID: (state, datasetCurID) => {
    //   localStorage.datasetCurID = datasetCurID
    //   state.datasetCurID = datasetCurID
    // },
    // setDatasetRole: (state, datasetRole) => {
    //   localStorage.datasetRole = datasetRole
    //   state.datasetRole = datasetRole
    // },
    // setTrainjobCurID: (state, trainjobCurID) => {
    //   localStorage.trainjobCurID = trainjobCurID
    //   state.trainjobCurID = trainjobCurID
    // }
  },
  getters: {
    token: state => {
      if (!state.token) {
        state.token = localStorage.token
      }
      return state.token
    },
    user: state => {
      if (!state.user) {
        state.user = localStorage.user
      }
      return state.user
    }
    // datasetCurID: state => {
    //   if (!state.datasetCurID) {
    //     state.datasetCurID = localStorage.datasetCurID
    //   }
    //   return state.datasetCurID
    // },
    // datasetRole: state => {
    //   if (!state.datasetRole) {
    //     state.datasetRole = localStorage.datasetRole
    //   }
    //   return state.datasetRole
    // },
    // trainjobCurID: state => {
    //   if (!state.trainjobCurID) {
    //     state.trainjobCurID = localStorage.trainjobCurID
    //   }
    //   return state.trainjobCurID
    // }
  }
})
// export default store
